package com.lyk.coursearrange.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyk.coursearrange.common.ServerResponse;
import com.lyk.coursearrange.common.log.annotation.Log;
import com.lyk.coursearrange.common.log.enums.OperationType;
import com.lyk.coursearrange.entity.Template;
import com.lyk.coursearrange.entity.dto.TemplateDto;
import com.lyk.coursearrange.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/template")
public class TemplateController {

    @Autowired
    private TemplateService templateService;

    /**
     * 查询所有模板
     *
     * @return
     */
    @PostMapping("/page")
    @Log(title = "分页查询模板列表", operationType = OperationType.SELECT)
    public ServerResponse page(@RequestBody TemplateDto templateDto) {
        if (templateDto.getTemplateStatus() != null) {
            LambdaQueryWrapper<Template> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Template::getTemplateStatus, templateDto.getTemplateStatus());
            List<Template> list = templateService.list(wrapper);
            return ServerResponse.ofSuccess(list);
        }else {
            List<Template> list = templateService.list();
            return ServerResponse.ofSuccess(list);
        }
    }

    /**
     * 添加模板信息
     *
     * @param templateDto
     * @return
     */
    @PostMapping("/add")
    @Log(title = "添加模板信息", operationType = OperationType.INSERT)
    public ServerResponse add(@RequestBody TemplateDto templateDto) {
        List templateArr = templateDto.getTemplateArr();
        String jsonString = JSON.toJSONString(templateArr);
        templateDto.setTemplateSet(jsonString);
        Boolean result = templateService.save(templateDto);
        if (result) {
            return ServerResponse.ofSuccess("添加成功");
        } else {
            return ServerResponse.ofError("添加失败");
        }
    }

    /**
     * 根据id获取信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getById/{id}")
    @Log(title = "根据id获取模板信息", operationType = OperationType.SELECT)
    public ServerResponse getById(@PathVariable("id") Integer id) {
        LambdaQueryWrapper<Template> wrapper = new LambdaQueryWrapper();
        wrapper.eq(Template::getTemplateId, id);
        Template template = templateService.getOne(wrapper);
        return ServerResponse.ofSuccess(template);
    }

    /**
     * 更新模板信息
     *
     * @param templateDto
     * @return
     */
    @PostMapping("/update")
    @Log(title = "更新模板信息", operationType = OperationType.UPDATE)
    public ServerResponse update(@RequestBody TemplateDto templateDto) {
        List templateArr = templateDto.getTemplateArr();
        String jsonString = JSON.toJSONString(templateArr);
        LambdaUpdateWrapper<Template> wrapper = Wrappers.<Template>lambdaUpdate()
                .set(Template::getTemplateName, templateDto.getTemplateName())
                .set(Template::getTemplateStatus, templateDto.getTemplateStatus())
                .set(Template::getTemplateType, templateDto.getTemplateType())
                .set(Template::getTemplateSet, jsonString)
                .eq(Template::getTemplateId, templateDto.getTemplateId());
        boolean update = templateService.update(wrapper);
        if (update) {
            return ServerResponse.ofSuccess("修改成功");
        } else {
            return ServerResponse.ofError("修改失败");
        }
    }

    @DeleteMapping("/delete/{id}")
    @Log(title = "删除模板", operationType = OperationType.DELETE)
    public ServerResponse delete(@PathVariable Long id) {
        LambdaQueryWrapper<Template> wrapper = new LambdaQueryWrapper();
        wrapper.eq(Template::getTemplateId, id);
        boolean remove = templateService.remove(wrapper);
        if (remove) {
            return ServerResponse.ofSuccess("删除成功");
        } else {
            return ServerResponse.ofError("删除失败");
        }
    }

}
